<template>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <div class="newCreate">新建任务</div>

        <el-form-item label="标题" prop="title">
            <el-input type="text" v-model="ruleForm.title" autocomplete="off" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="starttime">
            <el-date-picker v-model="ruleForm.starttime" type="date" placeholder="选择日期">
            </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endtime">
            <el-date-picker v-model="ruleForm.endtime" type="date" placeholder="选择日期">
            </el-date-picker>
        </el-form-item>
        <el-form-item label="重复类型" prop="type">
            <el-select v-model="ruleForm.type" placeholder="请选择">
                <el-option label="区域一" value="shanghai"></el-option>
                <el-option label="区域二" value="beijing"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="位置" prop="position">
            <el-select v-model="ruleForm.position" placeholder="请选择">
                <el-option label="朝阳区" value="chaoyang"></el-option>
                <el-option label="昌平区" value="changping"></el-option>
                <el-option label="海淀区" value="haidian"></el-option>
                <el-option label="大兴区" value="daxing"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="描述">
            <el-input type="textarea" :rows="6" placeholder="请输入内容" v-model="ruleForm.textarea">
            </el-input>
        </el-form-item>


        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">创建</el-button>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
    </el-form>
</template>


<script>
import { uploadTodoTodayApi } from '@/api/timeApi'

export default {
    props: ["setDialogVisible", 'dialogVisible'],
    data() {
        var validateTitle = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入标题'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validateStart = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        var validateEnd = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                title: '',
                starttime: '',
                endtime: '',
                type: '542',
                position: '区域2',
                textarea: '000'
            },
            rules: {
                checktitle: [
                    { validator: validateTitle, trigger: 'blur' }
                ],
                checkStart: [
                    { validator: validateStart, trigger: 'blur' }
                ],
                checkEnd: [
                    { validator: validateEnd, trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    // alert('创建成功');
                    let ret = await uploadTodoTodayApi(this.ruleForm)
                    if (ret.code === 0) {
                    console.log(ret);
                        this.$message({
                            message: '新增待办成功！',
                            type: 'success',
                            showClose: true,//手动关闭
                            duration: 3000,
                            onClose: () => {
                                this.setDialogVisible(false)
                            }
                        })
                    }
                } else {
                    console.log('请将表单填写完整');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.setDialogVisible(false)
        }
    }
}
</script>


<style scoped>
.newCreate {
    margin-left: 5px;
    margin-bottom: 40px;
    margin-top: -35px;
    border-bottom: 1px solid black;
    font-size: 18px;
}
</style>